package com._dev.social.controller;

import com._dev.social.common.BaseResponse;
import com._dev.social.common.ErrorCode;
import com._dev.social.common.ResultUtils;
import com._dev.social.mode.domain.Users;
import com._dev.social.mode.domain.request.UserLoginOrRegisterRequest;
import com._dev.social.service.FollowService;
import com._dev.social.service.UsersService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

import static com._dev.social.contant.UserConstant.USER_LOGIN_STATE;

/**
 * @program: social_median_backend
 * @description: 用户控制器类
 * @author: Mr.Liu
 * @create: 2024-11-05 09:18
 **/

@RestController

@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private FollowService followService;

    @PostMapping("/login")
    public BaseResponse<Users> userLogin(@RequestBody UserLoginOrRegisterRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null ||
                StringUtils.isAnyBlank(userLoginRequest.getEmail(), userLoginRequest.getPassword())) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        // 调用服务层处理登录
        Users user = usersService.userLogin(userLoginRequest.getEmail(), userLoginRequest.getPassword(), request);
        return ResultUtils.succeed(user);
    }

    //根据uid获取当前用户信息
    @GetMapping("/id")
    public BaseResponse<Users> findUserById(@RequestParam String uid) {
        if (StringUtils.isEmpty(uid) && uid == null) {
            return ResultUtils.error(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.succeed(usersService.findUserById(uid));
    }

    //获取当前用户信息
    @GetMapping("/current")
    public BaseResponse<Users> getCurrentUser(HttpServletRequest request) {
        Users currentUser = (Users) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (currentUser == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN);
        }
        Integer id = currentUser.getId();
        Users users = usersService.getById(id);
        //用户脱敏
        Users dto = usersService.getSafetyUser(users);
        return ResultUtils.succeed(dto);
    }

    @PostMapping("/register")
    public BaseResponse<String> userRegister(@RequestBody UserLoginOrRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            return ResultUtils.error(ErrorCode.NULL_ERROR);
        }
       usersService.userRegister(userRegisterRequest);
        return ResultUtils.succeed("注册成功");
    }

    @PostMapping("/logout")
    public Integer userLogin(HttpServletRequest request) {
        return usersService.userLogout(request);
    }

    @PostMapping("/followUser")
    public BaseResponse<String> followUser(@RequestParam String userId, @RequestParam String followUserId) {
        try {
            followService.followUser(userId, followUserId);
            return ResultUtils.succeed("关注成功");
        } catch (Exception e) {
            return ResultUtils.error(-1, "关注失败");
        }
    }

    @GetMapping("/getFollowList")
    public BaseResponse<Set<String>> getFollowList(@RequestParam String userId) {
        return ResultUtils.succeed(followService.getFollowList(userId));
    }

    @GetMapping("/getFollowedList")
    // 获取用户的被关注列表
    public BaseResponse<Set<String>> getFollowedList(@RequestParam String userId) {
        return ResultUtils.succeed(followService.getFollowedList(userId));
    }

    // 取消关注
    @GetMapping("/unfollowUser")
    public BaseResponse<String> unfollowUser(@RequestParam String userId, @RequestParam String unfollowUserId) {
        try {
            followService.unfollowUser(userId, unfollowUserId);
            return ResultUtils.succeed("取关成功");
        } catch (Exception e) {
            return ResultUtils.error(-1, "取消关注失败");
        }
    }

    @GetMapping("/list")
    public BaseResponse<List<Users>> getUsersList() {
        return ResultUtils.succeed(usersService.getListOfUser());
    }

}
